#include"Book.h"
#include <iostream>
#include<string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Tool.h"
using namespace LB;
using namespace std;

Book::Book(int m_bid,const char* m_bookname)
{
	this->m_bid=m_bid;
	borrowid = 0;
	restday = 0;
	orderid = 0;
	praise_val = 0;
	borrow_times = 0;
	strcpy(this->m_bookname,m_bookname);
}

Book::Book(int m_bid,const char* m_bookname,bool at_library,int restday,
		 int borrowid,int orderid,float praise_val,int borrow_times,vector<string> v_comment)
{
	this->m_bid=m_bid;
	strcpy(this->m_bookname,m_bookname);
	this->at_library = at_library;
	this->restday = restday;
	this->borrowid = borrowid;
	this->orderid = orderid;
	this->praise_val = praise_val;
	this->borrow_times = borrow_times;
	this->v_comment = v_comment;
}
Book::Book(const Book& that)
{
	strcpy(m_bookname,that.m_bookname);
	at_library=that.at_library;
	restday=that.restday;
	borrowid=that.borrowid;
	orderid=that.orderid;
	praise_val=that.praise_val;
	m_bid=that.m_bid;
	borrow_times=that.borrow_times;
	for (unsigned int i = 0; i < that.v_comment.size(); ++i)
	{
		char buf[60] = {};
		strcpy(buf,that.v_comment[i].c_str());
		this->v_comment.push_back(buf);
	}
}

void Book::operator = (const Book& that)
{
	this->m_bid=that.m_bid;
	strcpy(this->m_bookname,that.m_bookname);
	this->at_library = that.at_library;
	this->restday = that.restday;
	this->borrowid = that.borrowid;
	this->orderid = that.orderid;
	this->praise_val = that.praise_val;
	this->borrow_times = that.borrow_times;
	
	this->v_comment.clear();//再赋值构造里面　模本必须要清空　否则会拿原来的值

	for (unsigned int i = 0; i < that.v_comment.size(); ++i)
	{
		char buf[60] = {};
		strcpy(buf,that.v_comment[i].c_str());
		this->v_comment.push_back(buf);
	}

}

//重载 > 运算符 (根据praise_val的大小比较，方便后续排序算法)
bool Book::operator<(const Book& that) const
{
	if(praise_val>that.praise_val)
		return true;
	else
		return false; 
}

//重载 == 运算符(根据书名或书号判断，方便后续查找算法)
bool Book::operator==(const Book& that)const
{
	if(m_bid==that.m_bid) return true;
	return false;
}

//显示书籍信息：书号 书名 是否在馆 好评值 被借次数 借书人
void Book::showbook(void)
{
	char buff[255] = {};
	char buff1[255] = {};
	sprintf(buff1,"%d",orderid);
	sprintf(buff,"%d",borrowid);
	
	printf("╭═══════════════════════════════════════════\n"); 
	printf("│           <<%8s>>\n",m_bookname);
	printf("│  书号：%d \n",m_bid);  
	printf("│  状态：%s \n",(at_library) ? "在馆":"离馆"); 
	printf("│  借阅者：%s \n",(borrowid == 0 ? "无" : buff)); 
	printf("│  时间：%d (h) \n",(restday / 3600)); 
	printf("│  预订者：%s   \n",(orderid == 0 ? "无":buff1 ));  
	printf("│  好评等级: ");print_star(praise_val);
	printf("│  借阅次数：%d \n",borrow_times);  
	printf("│═══════════════════════════════════════════\n");   
	cout << "│  评论:"<< endl;
	showcomment();
	printf("╰════■□■□═══════════════════════════════■□■□\n");
	puts(""); 
	puts(""); 
	puts(""); 
}

//进来先判断是否为空，若为空则直接返回
void Book::insertcomment(int id,const char* comment)
{	
	if(NULL == comment) return;
	while(v_comment.size() >=5)
	{	
		v_comment.erase(v_comment.begin());
	}
	
	char cmt[60] = {};				
	sprintf(cmt,"用户%d:%s",id,comment);

	v_comment.insert(v_comment.end(),cmt);
	//v_comment.clear();
}

//显示近期书评
void Book::showcomment(void)
{
	for(unsigned int i=0;i<v_comment.size();i++)
	{	
		cout<<"│  "<<v_comment[i]<<endl;
	}
}

//关键字搜索判断
bool Book::findkey(char* keyword)
{
	if(strstr(m_bookname,keyword)!=NULL) return true;
	return false;
}

// 重载<< 运算符
namespace LB
{
	ostream& operator<<(ostream& os,const Book& that)
	{
		cout<<"书号："<<that.m_bid<<" ";
		cout<<"书名："<<that.m_bookname<<" "<<endl;
		cout<<"状态："<<that.at_library<<" ";
		cout<<"借阅者："<<that.borrowid<<" ";
		cout<<endl;
		cout<<"剩余天数："<<that.restday<<" "; 
		cout<<"预订者："<<that.orderid<<endl;
		return os;

	}
}